/*
 * PidginPluginLoader.java
 *
 * This file is (C) 2010 by the developers of pidgin-java.
 *
 * Pidgin, Finch, and libpurple are owned by their respective developers:
 * see http://pidgin.im
 *
 * This file is part of pidgin-java.
 *
 * pidgin-java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * pidgin-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with pidgin-java.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.google.code.pidginjava.core;

import java.io.*;
import java.util.jar.*;

public class PidginPluginLoader {
	
	/*
	 * Make the default constructor private to dissuade people from
	 * trying to instantiate it.
	 */
	private PidginPluginLoader() {}

	/*
	 * Given a filename, check if it refers to a pidgin plugin.
	 */
	static IPidginPlugin probe(String filename) {
		IPidginPlugin pp = null;
		
		/*
		 * Open the jar, look for the "Pidgin-Plugin" entry in the manifest,
		 * then load the class. If anything goes wrong, return null.
		 */
		try {
			JarFile jarFile = new JarFile(filename);
				
			try {
				Manifest manifest = jarFile.getManifest();

				if (null != manifest) {
					String pluginClassName = manifest.getMainAttributes().getValue("Pidgin-Plugin");
					
					if (null != pluginClassName) {
						try {
							pp = Class.forName(pluginClassName).asSubclass(IPidginPlugin.class).newInstance();
						}catch(IllegalAccessException iae) {
							
						}catch(InstantiationException ie) {
							
						}
						catch (ClassNotFoundException f) {

						}
					}
				}
			}
			finally {
				try { jarFile.close(); } catch (IOException e) {}
			}
		}
		catch (IOException e) {

		}
		
		
		return pp;
	}
}
